<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>计算器</title>
  <style>
  	tr{
		align:center;
	}
	button{
		width: 30px;
	}
  </style>
 </head>
 <body>
	<table border="1">
	  <tr>
		<td colspan="4"><input type="text" id="number"></td>
	  </tr>
	  <tr>
		<td><button value="←" onclick="back();">←</button></td>
		<td><button value="CE" onclick="ce();">CE</button></td>
		<td colspan="2" ><button value="=" style="width:70px;" onclick="jisuan();">=</button></td>
	  </tr>
	  <tr>
		<td><button value="1" onclick="btn('1');">1</button></td>
		<td><button value="2" onclick="btn('2');">2</button></td>
		<td><button value="3" onclick="btn('3');">3</button></td>
		<td><button value="+" onclick="btn('+');">+</button></td>
	  </tr>
	  <tr>
		<td><button value="4" onclick="btn('4');">4</button></td>
		<td><button value="5" onclick="btn('5');">5</button></td>
		<td><button value="6" onclick="btn('6');">6</button></td>
		<td><button value="-" onclick="btn('-');">-</button></td>
	  </tr>
	  <tr>
		<td><button value="7" onclick="btn('7');">7</button></td>
		<td><button value="8" onclick="btn('8');">8</button></td>
		<td><button value="9" onclick="btn('9');">9</button></td>
		<td><button value="*" onclick="btn('*');">*</button></td>
	  </tr>
	  <tr>
		<td colspan="2"><button value="0"  onclick="btn('0');" style="width:70px;">0</button></td>
		<td><button value="."  onclick="btn('.');">.</button></td>
		<td><button value="/"  onclick="btn('/');">/</button></td>
	  </tr>
	</table>
	<script type="text/javascript">
	var numberNode = document.getElementById("number");
	//单击控件，赋值
	function btn(t){
		if(numberNode.value == '0'){
			numberNode.value = t;
		}else{
			numberNode.value = numberNode.value+t;
		}
	}
	//后退方法
	function back(){
		val = numberNode.value;
		if(val.length>1){
			numberNode.value = numberNode.value.substring(0,numberNode.value.length-1);
		}else{
			numberNode.value=0;
		}
	}
	//清零
	function ce(){
		numberNode.value=0;
	}

	//计算
	function jisuan(){
		try{
			var t = eval("("+numberNode.value+")");
			numberNode.value = t;
		}catch(e){
			alert("表达式不正确，请检查");
		}
	}
	</script>
 </body>
</html>
